﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class XmlConstraintCollection : IXmlConstraintCollection
    {
        string elementname = null;

        List<IXmlConstraintProperty> ands = new List<IXmlConstraintProperty>();

        List<IXmlConstraintProperty> ors = new List<IXmlConstraintProperty>();


        public XmlConstraintCollection(string elementname)
        {
            this.elementname = elementname;
        }

        internal List<IXmlConstraintProperty> Ands
        {
            get
            {
                return ands;
            }
        }

        internal List<IXmlConstraintProperty> Ors
        {
            get
            {
                return ors;
            }
        }



        public void And(IXmlConstraintProperty property)
        {
            if (property == null)
                return;

            ands.Add(property);
        }

        public IXmlConstraintPropertyFactory<T> And<T>(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new Exception("missing propertyName.");

            propertyName = propertyName.Trim().ToUpper();

            return new XmlConstraintPropertyFactory<T>(elementname, propertyName, ands);
        }

        public IXmlConstraintPropertyFactory<string> And(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new Exception("missing propertyName.");

            propertyName = propertyName.Trim().ToUpper();

            return new XmlConstraintPropertyFactory<string>(elementname, propertyName, ands);
        }

        public void Or(IXmlConstraintProperty property)
        {
            if (property == null)
                return;

            ors.Add(property);
        }

        public IXmlConstraintPropertyFactory<T> Or<T>(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new Exception("missing propertyName.");

            propertyName = propertyName.Trim().ToUpper();

            return new XmlConstraintPropertyFactory<T>(elementname, propertyName, ors);
        }

        public IXmlConstraintPropertyFactory<string> Or(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new Exception("missing propertyName.");

            propertyName = propertyName.Trim().ToUpper();

            return new XmlConstraintPropertyFactory<string>(elementname, propertyName, ors);
        }



        public string ElementName
        {
            get
            {
                return elementname;
            }
        }

        public bool HasProperty
        {
            get
            {
                return ors.Count != 0 || ands.Count != 0;
            }
        }

        public override string ToString()
        {
            return elementname;
        }
    }
}
